include('shared.lua')

function GunvaultMenu(um)
	if GunvaultFrame then GunvaultFrame:Remove() end
	
	local guns = glon.decode(um:ReadString())
	local gunvaultid = um:ReadShort()

	local localweplist = weapons.GetList()
	
	GunvaultFrame = vgui.Create("DFrame")
	GunvaultFrame:SetSize(260, 415)
	GunvaultFrame:Center()
	
	if #guns == 0 then
		GunvaultFrame:SetTitle("Gunvault (Empty)")
	else
		GunvaultFrame:SetTitle("Gunvault (" .. table.Count(guns).. " items)")
	end
	
	GunvaultFrame:MakePopup()
	
	local GunList = vgui.Create("DPanelList")
		GunList:EnableVerticalScrollbar(true)
		GunList:SetParent(GunvaultFrame)
		GunList:SetPos(5, 27)
		GunList:SetSize(GunvaultFrame:GetWide() - 10, GunvaultFrame:GetTall() - 33)
		GunList:SetPadding(5)
		GunList:SetSpacing(5)
	
	for k, v in pairs(guns) do
		local Panel = vgui.Create("DPanel")
		
		local Label = vgui.Create("DLabel")
			Label:SetParent(Panel)
			Label:SetPos(5, 2)
			Label:SetText(localweplist[v].PrintName)
			Label:SetFont("GmodNotify")
			Label:SetTextColor(Color(255, 255, 255))
		
		local Button = vgui.Create("DButton")
			Button:SetParent(Panel)
			Button:SetSize(69, 20)
			Button:SetPos(GunList:GetWide() - 140, 2)
			Button:SetText("Withdraw")
			function Button.DoClick()
				RunConsoleCommand("Gunvaultwithdraw", gunvaultid, k, v)
			end
		local Button = vgui.Create("DButton")
			Button:SetParent(Panel)
			Button:SetSize(34, 20)
			Button:SetPos(GunList:GetWide() - 50, 2)
			Button:SetText("Give")
			function Button.DoClick()
				RunConsoleCommand("Gunvaultgive", gunvaultid, k, v)
		end
		GunList:AddItem(Panel)
	end
end
usermessage.Hook("gunvaultmenu", GunvaultMenu)
